package com.zerosky.crm.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zerosky.crm.po.Employee;
import com.zerosky.crm.po.ServiceSupport;
import com.zerosky.crm.service.ServiceSupportService;
import com.zerosky.crm.util.BaseAction;
import com.zerosky.crm.util.PageInfo;

/**
 * 服务支持相关业务的Controller
 * 
 * @author 钟璟
 */
@Controller
public class ServiceSupportController extends BaseAction {
	private Logger logger = LoggerFactory.getLogger(ServiceSupportController.class); // 日志

	@Resource(name = "serviceSupportService")
	private ServiceSupportService serviceSupportService;

	/**
	 * 去服务支持管理
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("toServiceJsp.do")
	public String toServiceJsp(HttpSession session, HttpServletRequest request) {
		if (checkPower(session, 109)) {
			return "WEB-INF/jsp/system/serviceSupport";
		} else {
			return "WEB-INF/jsp/error/404";
		}
	
	}

	/**
	 * 去服务支持列表
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("toServiceSupportJsp.do")
	public String toServiceSupportJsp(HttpSession session, HttpServletRequest request) {
		return "WEB-INF/jsp/serviceSupport";
	}

	/**
	 * 去服务支持列表
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("toListServiceSupport.do")
	public String toListServiceSupport(HttpSession session, HttpServletRequest request) {
		return "WEB-INF/jsp/ServiceSupport/listServiceSupport";
	}

	@RequestMapping("/queryServiceSupportList.do")
	@ResponseBody
	public Map<String, Object> queryServiceSupportList(HttpSession session, HttpServletRequest request, Integer limit,
			Integer page, String ServiceSupportName) throws Exception {
		PageInfo<ServiceSupport> pageInfo = new PageInfo<ServiceSupport>();
		Map<String, Object> json = new HashMap<String, Object>();
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			map.put("serviceName", ServiceSupportName);
			map.put("pageNo", page);
			map.put("pageSize", limit);
			pageInfo = serviceSupportService.getPageInfoList(map);

			json.put("data", pageInfo.getList());
			json.put("total", pageInfo.getPageCount());
			json.put("code", 0);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", -1);
			logger.error("获取数据失败，原因：" + e);
		}
		return json;
	}

	@RequestMapping(value = "/saveServiceSupport.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> saveOrUpdateServiceSupport(ServiceSupport ServiceSupport, HttpServletRequest request,
			HttpServletResponse response, HttpSession session) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			// 获取登录用户
			Employee user = (Employee) session.getAttribute("loginUser");
			ServiceSupport.setServiceId(UUID.randomUUID().toString());
			ServiceSupport.setCreateDate(new Date());
			ServiceSupport.setCreateEmpId(user.getEmployeeId());

			Integer code = serviceSupportService.saveOrUpdate(ServiceSupport, "新增");

			if (code > 0) {
				map.put("code", 0);
			} else {
				map.put("code", -2);
			}
		} catch (Exception e) {
			map.put("code", -1);
			logger.error("获取数据失败，原因：" + e);
		}
		return map;
	}

	@RequestMapping(value = "deleteServiceSupport.do")
	@ResponseBody
	public Map<String, Object> deleteServiceSupport(HttpSession session, HttpServletRequest request,
			HttpServletResponse response, String ServiceSupportId) throws Exception {
		Map<String, Object> map = new HashMap<>();
		try {
			Integer code = serviceSupportService.deleteService(ServiceSupportId);
			if (code > 0) {
				map.put("code", 0);
			} else {
				map.put("code", -2);
			}
		} catch (Exception e) {
			map.put("code", -1);
			logger.error("获取数据失败，原因：" + e);
		}
		return map;
	}

	/**
	 * 
	 * 修改前根据id查询出来修改的服务支持
	 * 
	 * @param request
	 * @param ServiceSupportId
	 * @return
	 */
	@RequestMapping("toModifyServiceSupport.do")
	@ResponseBody
	public Map<String, Object> toModifyServiceSupport(HttpSession session, HttpServletRequest request,
			String ServiceSupportId) {
		Map<String, Object> json = new HashMap<String, Object>();
		try {
			ServiceSupport ServiceSupport = serviceSupportService.getServiceById(ServiceSupportId);
			if (ServiceSupport != null) {
				// 数据
				json.put("code", 0);
				json.put("data", ServiceSupport);
			} else {
				json.put("code", -2);
			}

		} catch (Exception e) {
			json.put("code", -1);
			logger.error("获取数据失败，原因：" + e);
		}
		return json;
	}

	@RequestMapping("modifyServiceSupport.do")
	@ResponseBody
	public Map<String, Object> modifyServiceSupport(HttpSession session, HttpServletRequest request,
			ServiceSupport ServiceSupport) {
		Map<String, Object> json = new HashMap<String, Object>();
		try {
			Integer count = serviceSupportService.saveOrUpdate(ServiceSupport, "修改");
			if (count > 0) {
				json.put("code", 0);
			} else {
				json.put("code", -2);
			}
		} catch (Exception e) {
			json.put("code", -1);
			logger.error("获取数据失败，原因：" + e);
		}
		return json;
	}

}
